﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class WuLiaoGuanLi : BaseForm
    {
        private bool m_IsChuKu = false;
        public WuLiaoGuanLi(bool IsChuKu)
        {
            InitializeComponent();
            m_IsChuKu = IsChuKu;
            if (m_IsChuKu)
            {
                this.Text = "入库物料管理";
            }
            else
            {
                this.Text = "出库物料管理";
            }
        }

        private void WuLiaoGuanLi_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            BindWuLiao();
            BindGuiGe();
            if (string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("偏差设置"))
            {
                col_pianchazhongliang.Visible = true;
                col_pianchazhuangtai.Visible = true;
            }
            else
            {
                col_pianchazhongliang.Visible = false;
                col_pianchazhuangtai.Visible = false;
            }
        }

        private void menu_1_xinzeng_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }

            WuLiaoBianJi wuLiaoBianJi = new WuLiaoBianJi(m_IsChuKu);
            if (wuLiaoBianJi.ShowDialog() == DialogResult.OK)
            {
                BindWuLiao();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
                BindGuiGe();
            }
        }

        private void menu_1_xiugai_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView1.SelectedRows .Count==0)
            {
                return;
            }
            int selectIndex = dataGridView1.SelectedRows[0].Index;
            WuLiaoBianJi wuLiaoBianJi = new WuLiaoBianJi(dataGridView1.SelectedRows[0],m_IsChuKu);
            if (wuLiaoBianJi.ShowDialog() == DialogResult.OK)
            {
                BindWuLiao();
                dataGridView1.Rows[selectIndex].Selected = true;
                BindGuiGe();
            }
        }

        private void menu_1_shanchu_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView1.SelectedRows.Count ==0)
            {
                return;
            }

            if (dataGridView2.SelectedRows.Count != 0)
            {
                KeaoUtils.ShowInfoMessage("请先删除该物料下所有型号信息。");
                return;
            }

            if (KeaoUtils.ShowQuestionMessage("确定要删除该物料信息吗？") == DialogResult.Yes)
            {
                if (dataGridView2.Rows != null)
                {
                    foreach (DataGridViewRow row in dataGridView2.Rows)
                    {
                        TableRow deleteXingHaoRow = new TableRow() {TableName="tbl_xinghao" ,ID =string.Format("{0}",row.Cells["col_xinghao_id"].Value) };
                        KeaoUtils._DB.DeleteRowByID(deleteXingHaoRow);
                    }
                }

                TableRow deleteWuLiaoRow = new TableRow() { TableName = "tbl_wuliao", ID = string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value) };
                if (KeaoUtils._DB.DeleteRowByID(deleteWuLiaoRow) > 0)
                {
                    KeaoUtils.ShowInfoMessage("删除物料信息完成。");
                    BindWuLiao();
                }

            }
        }

        private void menu_2_zengjiaguige_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }

            if (dataGridView1.SelectedRows .Count ==0)
            {
                return;
            }
            WuLiaoXingHaoBianJi wuLiaoXingHaoBianJi = new WuLiaoXingHaoBianJi(string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_wuliaomingcheng"].Value), string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value),string.Format("{0}",dataGridView1.SelectedRows[0].Cells["col_wuliaoleixing"].Value));
            if (wuLiaoXingHaoBianJi.ShowDialog() == DialogResult.OK)
            {
                BindGuiGe();
                dataGridView2.Rows[dataGridView2.Rows.Count - 1].Selected = true;
            }
        }

        private void menu_2_xiugaiguige_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView2.SelectedRows.Count==0)
            {
                return;
            }
            int selectIndex = dataGridView2.SelectedRows[0].Index;
            WuLiaoXingHaoBianJi wuLiaoXingHaoBianJi = new WuLiaoXingHaoBianJi(string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_wuliaomingcheng"].Value), string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value), string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_wuliaoleixing"].Value), dataGridView2.SelectedRows[0]);
            if (wuLiaoXingHaoBianJi.ShowDialog() == DialogResult.OK)
            {
                BindGuiGe();
                dataGridView2.Rows[selectIndex].Selected = true;
            }
        }

        private void menu_2_shanchuguige_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("物料管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView2.SelectedRows.Count == 0)
            {
                return;
            }
            if (KeaoUtils.ShowQuestionMessage("确定要删除该规格型号吗？") == DialogResult.Yes)
            {
                TableRow deleteRow = new TableRow() { TableName="tbl_xinghao", ID=string.Format("{0}",dataGridView2.SelectedRows[0].Cells["col_xinghao_id"].Value) };
                if (KeaoUtils._DB.DeleteRowByID(deleteRow)>0)
                {
                    KeaoUtils.ShowInfoMessage("删除规格型号完成。");
                    BindGuiGe();
                }
            }
        }

        private void BindWuLiao()
        {
            string createCondition = string.Empty;
            if (m_IsChuKu)
            {
                createCondition = KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, "入库物料", "", SearchConditionType.等于);
            }
            else
            {
                createCondition = KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, "出库物料", "", SearchConditionType.等于);
            }

            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", createCondition);
            dataGridView1.DataSource = Dt;
        }

        private void BindGuiGe()
        {
            if (dataGridView1.SelectedRows.Count==0)
            {
                dataGridView2.DataSource = null;
                return;
            }
            string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value), "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", createCondition);
            dataGridView2.DataSource = Dt;
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value =string.Format("{0}",e.Row.Index + 1);
        }

        private void dataGridView2_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}",e.Row.Index + 1);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            BindGuiGe();
        }

    }
}
